package com.nageoffer.jobtrain.projects.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.nageoffer.jobtrain.common.web.result.Result;
import com.nageoffer.jobtrain.common.web.result.Results;
import com.nageoffer.jobtrain.projects.annotation.NoUserBlockingAction;
import com.nageoffer.jobtrain.projects.dto.req.OjQuestionExecuteResultReqDTO;
import com.nageoffer.jobtrain.projects.dto.req.OjQuestionPageListReqDTO;
import com.nageoffer.jobtrain.projects.dto.req.OjQuestionSubmitReqDTO;
import com.nageoffer.jobtrain.projects.dto.resp.*;
import com.nageoffer.jobtrain.projects.handler.CustomBlockHandler;
import com.nageoffer.jobtrain.projects.service.OjQuestionService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * OJ题目控制层
 * author：唐晨
 */
@RestController
@RequiredArgsConstructor
public class OjQuestionController {
    private final OjQuestionService ojQuestionService;

    /**
     * OJ题目分页请求 -匹配关键词、题目难度
     */
    @PostMapping("/api/job-train/project/oj/page-list")
    public Result<OjQuestionPageListRespDTO> ojQuestionPageList(@RequestBody OjQuestionPageListReqDTO requestParam) {
        return Results.success(ojQuestionService.ojQuestionPageList(requestParam));
    }

    /**
     * 根据OJ题目ID，获取OJ题目详情
     */
    @NoUserBlockingAction(message = "用户状态已拉黑，禁止答题，请先联系管理员！")
    @GetMapping("/api/job-train/project/oj/detail")
    public Result<OjQuestionDetailRespDTO> ojQuestionDetail(@RequestParam("id") Long id) {
        return Results.success(ojQuestionService.ojQuestionDetail(id));
    }

    /**
     * 获取上一道OJ题目详情
     */
    @GetMapping("/api/job-train/project/oj/prev-detail")
    public Result<OjQuestionPrevDetailRespDTO> ojQuestionPrevDetail(@RequestParam("id") Long id) {
        return Results.success(ojQuestionService.ojQuestionPrevDetail(id));
    }

    /**
     * 获取下一道OJ题目详情
     */
    @GetMapping("/api/job-train/project/oj/next-detail")
    public Result<OjQuestionNextDetailRespDTO> ojQuestionNextDetail(@RequestParam("id") Long id) {
        return Results.success(ojQuestionService.ojQuestionNextDetail(id));
    }

    /**
     * 用户提交代码逻辑
     */
    @PostMapping("/api/job-train/project/oj/submit-code")
    public Result<OjQuestionSubmitRespDTO> ojQuestionSubmitCode(@RequestBody OjQuestionSubmitReqDTO requestParam) {
        return ojQuestionService.ojQuestionSubmitCode(requestParam);
    }

    /**
     * 用户提交代码逻辑 -MQ逻辑 - 添加Sentinel限流功能
     */
    @PostMapping("/api/job-train/project/oj/submit-code/by-rocketmq")
    @SentinelResource(
            value = "oj-question-submit-code",
            blockHandler = "ojQuestionSubmitCodeByRocketMQ",
            blockHandlerClass = CustomBlockHandler.class)
    public Result<Void> ojQuestionSubmitCodeByRocketMQ(@RequestBody OjQuestionSubmitReqDTO requestParam) {
        ojQuestionService.ojQuestionSubmitCodeByRocketMQ(requestParam);
        return Results.success();
    }

    /**
     * 用户提交判题结果获取 -通过前端定时器
     */
    @PostMapping("/api/job-train/project/oj/execute-result")
    public Result<OjQuestionSubmitRespDTO> ojQuestionExecuteResult(@RequestBody OjQuestionExecuteResultReqDTO requestParam) {
        return Results.success(ojQuestionService.ojQuestionExecuteResult(requestParam));
    }

    /**
     * 获取答题热榜排行功能
     */
    @PostMapping("/api/job-train/project/oj/hot-list")
    public Result<List<String>> ojQuestionHotList() {
        return Results.success(ojQuestionService.ojQuestionHotList());
    }
}